﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using WimaxSimulator.Properties;

namespace WimaxSimulator.Wimax
{
    public class Sink : Station
    {
        private uint ip;
        public Sink(int id, uint ip)
            : base(id, new Point(0, 0), 0)
        {
            this.ip = ip;
        }
        private BaseStation getNextHop(Packet p)
        {
            return (from bs in
                 SimulationEngine.BaseStations
             where ((p.Destination.Ip ^ bs.Ip) & bs.IpAddressBlock.SubnetMask) == 0
             select bs).SingleOrDefault<BaseStation>();
        }
        public override void Receive(Packet p)
        {
            SimulationEngine.ReceivedData[Id] += p.Size;
            p.LastStation = this;
            BaseStation b = getNextHop(p);
            if (b != null)
                b.Receive(p);
        }

        public override void Send()
        {
            List<Traffic> traffics = (from t in TrafficDestinations where t.StartTime <= SimulationEngine.SimulationTime && t.EndTime >= SimulationEngine.SimulationTime select t).ToList<Traffic>();

            foreach (Traffic t in traffics)
            {
                Station dest = SimulationEngine.GetStationById(t.Destination);
                if (dest.Ip == 0)
                    continue;
                Packet p = new Packet(this, dest, Settings.Default.MsTotalBw) { LastStation = this ,StartTime=SimulationEngine.SimulationTime};
                getNextHop(p).Receive(p);
            }
        }
        public void Connect(BaseStation station)
        {
        }

        public override uint Ip
        {
            get { return ip; }
        }
    }
}
